package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
    import org.flixel.*;
  
    // Overall level terrain
    public class Platform extends FlxTileblock {

		[Embed(source = "inktile2.png")]
		private var texture:Class;
		
        // Constructor to create a platform (rectangle)
        public function Platform(X:Number, Y:Number, Width:Number, Height:Number, Visible:Boolean, Ground:Boolean=true) {
            // Create sprite at coordinate X, Y
            super(X, Y, Width, Height);
			blend = 'layer';
			

            // bounding box
            width = Width;
            height = Height;

            immovable = true;
            visible = Visible;
			
			
			
            //makeGraphic(Width, Height, 0xff000000);
			if ( Ground )
			{
				pixels = new BitmapData(width, height, true, 0x00000000);
				framePixels = pixels;
				// Manipulate the corners
				for ( var i:int = 0; i < Width; i++ )
				{
					for ( var j:int = 0; j < Height; j++ )
					{
						// Generate color (we want some patterns to get a smeary effect)
						var Color:uint = (Math.random() * 16 + 4 * i) % 32;
						Color = Color << 8;
						Color = Color & 0x0000FF00;
						Color += (Math.random() * 16 + j) % 48;
						Color = Color << 8;
						Color = Color & 0x00FFFF00;
						Color += (Math.random() * 16 + i * j) % 64;
						Color &= 0x00FFFFFF;
							
						// Detect top-left
						if ( i < 3 && j < 3 )
						{
							var dist:Number = (i + j) / 5;
							var alpha:int = dist * 255;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( i >= Width-3 && j < 3 ) // Top-right
						{
							var iOff:int = Width - i - 1;
							var dist:Number = (iOff + j) / 5;
							var alpha:int = dist * 255;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( i < 3 && j >= Height-3 ) // Bottom-left
						{
							var jOff:int = Height - j - 1;
							var dist:Number = (i + jOff) / 5;
							var alpha:int = dist * 255;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( i >= Width-3 && j >= Height-3 ) // Bottom-left
						{
							var jOff:int = Height - j - 1;
							var iOff:int = Width - i - 1;
							var dist:Number = (iOff + jOff) / 5;
							var alpha:int = dist * 255;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( i < 3 )			// Left side
						{
							var dist:Number = i * i / 4;
							var alpha:int = dist * 128;
							alpha += 128;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( j < 3 )			// Top side
						{
							var dist:Number = j * j / 4;
							var alpha:int = dist * 128;
							alpha += 128;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( j >= Height-3 )			// Bottom side
						{
							var jOff:int = Height - j - 1;
							var dist:Number = jOff * jOff / 4;
							var alpha:int = dist * 128;
							alpha += 128;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else if ( i >= Width-3 )			// Right side
						{
							var iOff:int = Width - i - 1;
							var dist:Number = iOff * iOff / 4;
							var alpha:int = dist * 128;
							alpha += 128;
							alpha = alpha * alpha / 128;
							if ( alpha > 255 )
								alpha = 255;
								
							alpha = alpha << 24;
							alpha &= 0xFF000000;
							Color |= alpha;
							super.pixels.setPixel32(i, j, Color);
						}
						else
						{
							Color |= 0xFF000000;
							super.pixels.setPixel32(i, j, Color);
						}
					}
				}
			}
				
			
			else
			{
				loadTiles(texture, 10, 10, 0);

			}
			//{
				//Generate color (we want some patterns to get a smeary effect)
				//pixels = new BitmapData(width, height, true, 0xFF000000);
				//framePixels = pixels;
				//
				//var rOffsetX:int = int(Math.random() * 30) - 15;
				//var rOffsetY:int = int(Math.random() * 30) - 15;
				//var gOffsetX:int = int(Math.random() * 30) - 15;
				//var gOffsetY:int = int(Math.random() * 30) - 15;
				//var bOffsetX:int = int(Math.random() * 30) - 15;
				//var bOffsetY:int = int(Math.random() * 30) - 15;
				//
				//var fadeWidth:int = 16;
				//
				//for ( var i:int = 0; i < width; i++ )
				//{
					//for ( var j:int = 0; j < height; j++ )
					//{
						//var coordi:Number = (x + i) % 60 - 30;
						//var coordj:Number = (y + j) % 60 - 30;
						//var absi:Number = x + i;
						//var absj:Number = y + j;
						//
						 //If we're near an edge, dial back the colors
						 //Calculate edge distance
						//var edgeDist = 2*fadeWidth;
						///*if ( i < fadeWidth && j < fadeWidth )
						//{
							//edgeDist = i + j;
							//edgeDist *= 0.5;
						//}
						//else if ( i < fadeWidth && j >= height-fadeWidth )
						//{
							//edgeDist = i + (height - j);
							//edgeDist *= 0.5;
						//}
						//else if ( i >= width - fadeWidth && j < fadeWidth )
						//{
							//edgeDist = (width - i) + j;
							//edgeDist *= 0.5;
						//}
						//else if ( i >= width - fadeWidth && j >= height - fadeWidth )
						//{
							//edgeDist = (width - i) + (height - j);
							//edgeDist *= 0.5;
						//}
						//else if ( i < fadeWidth )
						//{
							//edgeDist = i * 2;
						//}
						//else if ( i >= width - fadeWidth )
						//{
							//edgeDist = (width - i) * 2;
						//}
						//else if ( j < fadeWidth )
						//{
							//edgeDist = j * 2;
						//}
						//else if ( j >= height - fadeWidth )
						//{
							//edgeDist = (height - j) * 2;
						//}
						//else
							//edgeDist = 2*fadeWidth;*/
						//
						//
						 //Scale edgeDist.  It is in the range [0,2*edgeDist] at this point
						//var distScalar:Number = 0.375;
						//edgeDist *= distScalar;
						//
						//var Color:uint = Math.abs( ((absj + absi) % 121) + (coordi+rOffsetX) * (coordi+rOffsetX) + (coordj+rOffsetY) * (coordj+rOffsetY) - 255) < 20 ? 
									//(Math.random() * edgeDist * 2) + edgeDist*2 : 
									//((absi * absj) % 48) * 0.35;// * (edgeDist / (2 * fadeWidth) * distScalar);
						//Color = Color << 8;
						//Color = Color & 0x0000FF00;
						//Color += Math.abs( ((absj + absi) % 121) + (coordi+gOffsetX) * (coordi+gOffsetX) + (coordj+gOffsetY) * (coordj+gOffsetY) - 255) < 20 ? 
									//(Math.random() * edgeDist * 2) + edgeDist*2 : 
									//((absi * absj) % 48) * 0.65;// * (edgeDist / (1.5 * fadeWidth) * distScalar);
						//Color = Color << 8;
						//Color = Color & 0x00FFFF00;
						//Color += Math.abs( ((absj + absi) % 121) + (coordi+bOffsetX) * (coordi+bOffsetX) + (coordj+bOffsetY) * (coordj+bOffsetY) - 255) < 20 ? 
									//(Math.random() * edgeDist * 2) + edgeDist*2 : 
									//((absi * absj) % 48) * 0.4;// * (edgeDist / (2 * fadeWidth) * distScalar);
						//Color |= 0xFF000000;
						//
						//pixels.setPixel32(i, j, Color);
					//}
				//}
			//}
		//
			

			
			drag = new FlxPoint(1000000, 1000000);
        }
    }
}